//
//  SearcherViewController.m
//  礼物
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "SearcherViewController.h"
#import "MySearchTableViewCell.h"
@interface SearcherViewController ()
@end

@implementation SearcherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    
    
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, 375, 44)];
    self.searchBar.delegate = self;

    self.myTableView = [[UITableView alloc] initWithFrame:MAINSCREEN style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.tableHeaderView = self.searchBar;
    self.myTableView.sectionHeaderHeight=30*HEIGHT/667;
    
   
    
}
-(void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30*HEIGHT/667)];
    SearchModel *model=[self.searchArray objectAtIndex:section];
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:model.name];
    [att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[model.name rangeOfString:[self.searchBar text]]];
    label.attributedText=att;
    return label;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.searchArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MySearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        
        cell = [[MySearchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    SearchModel *model=[self.searchArray objectAtIndex:indexPath.section];
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:model.desc];
    [att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[model.desc rangeOfString:[self.searchBar text]]];
    cell.att=att;
    [cell setModel:model];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NextViewController *nextVC = [[NextViewController alloc] init];
    SearchModel *model=[self.searchArray objectAtIndex:indexPath.section];
    nextVC.model=model;
    nextVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:nextVC animated:YES];
}
//让UITableView的section header view不悬停的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight =30*HEIGHT/667;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150*HEIGHT/667;
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    NSString *str=[searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.bohejiaju.com/v1/search/item?keyword=%@&limit=20&offset=0&sort=",str] andBlock:^(id data) {
        //        NSLog(@"====%@",data);
        self.searchArray=[NSMutableArray array];
        NSMutableDictionary *Dic=data;
        NSMutableDictionary *Dic1=[Dic objectForKey:@"data"];
        NSMutableArray *array=[Dic1 objectForKey:@"items"];
        for (NSMutableDictionary *dic in array) {
            SearchModel *model=[[SearchModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            model.desc=[dic objectForKey:@"description"];
            [self.searchArray addObject:model];
        }
        [self.myTableView reloadData];
    } andError:^(id failure) {}];

}

//// 网络搜索根据关键字转化为NSUTF8StringEncoding来搜索
//-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
//    
//    NSString *searchString = [self.searchController.searchBar text];
//    NSString *str=[searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.bohejiaju.com/v1/search/item?keyword=%@&limit=20&offset=0&sort=",str] andBlock:^(id data) {
////        NSLog(@"====%@",data);
//        self.searchArray=[NSMutableArray array];
//        NSMutableDictionary *Dic=data;
//        NSMutableDictionary *Dic1=[Dic objectForKey:@"data"];
//        NSMutableArray *array=[Dic1 objectForKey:@"items"];
//        for (NSMutableDictionary *dic in array) {
//            SearchModel *model=[[SearchModel alloc]init];
//            [model setValuesForKeysWithDictionary:dic];
//            model.desc=[dic objectForKey:@"description"];
//            [self.searchArray addObject:model];
//        }
//        [self.myTableView reloadData];
//    } andError:^(id failure) {}];
//}
//
//
////UISearchController的移除
////在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    if (self.searchController.active) {
//        self.searchController.active = NO;
//        [self.searchController.searchBar removeFromSuperview];
//    }
//}
//
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
